define(function(){
	var ListForm=function (initConfig){
		var listForm=this;

		listForm.callback=initConfig.callback || {};
		listForm.data={};

		listForm.$dom=$('<div class="listFormBox"></div>');
		listForm.$listForm=$('<ul class="listForm"></ul>').appendTo(listForm.$dom);

		!initConfig.formClassName && (initConfig.formClassName='');
		listForm.$dom.addClass(initConfig.formClassName);

		if(initConfig.formStyle!==undefined){
			listForm.$dom.css(initConfig.formStyle);
		}

		listForm.FormRoot=initConfig.FormRoot || function(){};
		listForm.formRoot=new listForm.FormRoot();

		listForm.liDomList=[];

		function FormLiDom(liTemplete){
			var formLiDom=this;
			formLiDom.$dom=$('<li class="formLi"></li>');
			formLiDom.$liTitle=$('<div class="liTitle"></div>').appendTo(formLiDom.$dom);
			formLiDom.$liTitleText=$('<div class="titleText"></div>').appendTo(formLiDom.$liTitle);
			formLiDom.$liContent=$('<div class="liContent"></div>').appendTo(formLiDom.$dom);
			formLiDom.$liTitleText.html(liTemplete.title);
			if(liTemplete.content.init!==undefined){
				liTemplete.content.init(formLiDom.$liContent,listForm.formRoot,listForm,formLiDom);
			};
			formLiDom.update=function(formData){
				if(liTemplete.content.update!==undefined){
					liTemplete.content.update(formLiDom.$liContent,listForm.formRoot,formData);
				};
			}
		}



		listForm.addLi=function(liTemplete,isPrepend){
			var formLiDom=new FormLiDom(liTemplete);
			if(isPrepend===true){
				formLiDom.$dom.prependTo(listForm.$listForm);
			}else{
				formLiDom.$dom.appendTo(listForm.$listForm);
			}
			listForm.liDomList.push(formLiDom);
		}
		listForm.appendLi=function(){
			var liTempleteList=Array.prototype.slice.call(arguments);
			$.each(liTempleteList, function(i,liTemplete) {
				listForm.addLi(liTemplete);
			});
		}
		listForm.update=function(formData){
			if(formData!==undefined){
				listForm.data=formData;
			}
			$.each(listForm.liDomList, function(i,formLiDom) {
				formLiDom.update(listForm.data);
			});
		}
		listForm.submit=function(){
			if(typeof listForm.callback.submit === 'function'){
				listForm.callback.submit(listForm.formRoot);
			}
		}

		if(initConfig.li!==undefined){
			listForm.appendLi.apply(listForm.appendLi,initConfig.li);
		}

		if(initConfig.submitBtnText!==false){
			listForm.$fromFooter=$('<div class="formFooter"></div>').appendTo(listForm.$dom);
			listForm.$submitBtn=$('<span class="btn btn-primary btn-submit"></span>').appendTo(listForm.$fromFooter);
			listForm.$submitBtn.html(initConfig.submitBtnText || '提交');
			listForm.$submitBtn.on('click',function(){
				listForm.submit();
			});
		}


	}
	window.ListForm=ListForm;
	return ListForm;
})
